package com.zhijian.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhijian.medical.dao.local.ExamWarehouseStudyMapper;
import com.zhijian.medical.entity.po.ExamWarehouseStudy;
import com.zhijian.medical.entity.vo.request.ExamStartVo;
import com.zhijian.medical.entity.vo.request.ExamWarehouseStudySaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.ExamWarehouseStudySearchVo;
import com.zhijian.medical.entity.vo.response.ExamDoingVo;
import com.zhijian.medical.entity.vo.response.ExamWarehouseStudyVo;
import com.zhijian.medical.enums.ResponseCodeEnum;
import com.zhijian.medical.exception.BusinessException;
import com.zhijian.medical.service.ExamPaperService;
import com.zhijian.medical.service.ExamWarehouseStudyService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 学习模块-题库关联关系表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-04-01 15:01:57
 */
@Slf4j
@Service
public class ExamWarehouseStudyServiceImpl implements ExamWarehouseStudyService {

    @Autowired
    private ExamWarehouseStudyMapper mapper;

    @Autowired
    private ExamPaperService examPaperService;

    @Override
    public ExamWarehouseStudy selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExamWarehouseStudyVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(ExamWarehouseStudy examWarehouseStudy) {
        Date now = new Date();
        examWarehouseStudy.setCreateTime(now);
        examWarehouseStudy.setUpdateTime(now);
        return mapper.insert(examWarehouseStudy) > 0 ? examWarehouseStudy.getId() : 0L;
    }

    @Override
    public Long insert(ExamWarehouseStudySaveOrUpdateVo examWarehouseStudySaveOrUpdateVo) {
        ExamWarehouseStudy examWarehouseStudy = new ExamWarehouseStudy();
        BeanUtils.copyProperties(examWarehouseStudySaveOrUpdateVo, examWarehouseStudy);
        if (StringUtils.isNotBlank(examWarehouseStudySaveOrUpdateVo.getStartTime())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            examWarehouseStudy.setStartTime(DateUtil.parse(examWarehouseStudySaveOrUpdateVo.getStartTime(), sdf));
        }
        return this.insert(examWarehouseStudy);
    }


    @Override
    public boolean update(ExamWarehouseStudy examWarehouseStudy) {
        Date now = new Date();
        examWarehouseStudy.setUpdateTime(now);
        return mapper.updateByPrimaryKey(examWarehouseStudy) != 0;
    }

    @Override
    public boolean update(ExamWarehouseStudySaveOrUpdateVo examWarehouseStudySaveOrUpdateVo) {
        ExamWarehouseStudy examWarehouseStudy = new ExamWarehouseStudy();
        BeanUtils.copyProperties(examWarehouseStudySaveOrUpdateVo, examWarehouseStudy);
        if (StringUtils.isNotBlank(examWarehouseStudySaveOrUpdateVo.getStartTime())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            examWarehouseStudy.setStartTime(DateUtil.parse(examWarehouseStudySaveOrUpdateVo.getStartTime(), sdf));
        } else {
            examWarehouseStudy.setStartTime(null);
        }
        return this.update(examWarehouseStudy);
    }

    @Override
    public Integer count(ExamWarehouseStudySearchVo examWarehouseStudySearchVo) {
        return mapper.count(examWarehouseStudySearchVo);
    }

    @Override
    public List<ExamWarehouseStudyVo> selectList(ExamWarehouseStudySearchVo examWarehouseStudySearchVo) {
        return mapper.selectList(examWarehouseStudySearchVo);
    }

    @Override
    public Page<ExamWarehouseStudyVo> selectPage(ExamWarehouseStudySearchVo examWarehouseStudySearchVo) {
        //先直接查总数
        int count = count(examWarehouseStudySearchVo);
        //分页
        Page<ExamWarehouseStudyVo> page = new Page<>(true, count, examWarehouseStudySearchVo.getPageNum(), examWarehouseStudySearchVo.getPageSize());
        examWarehouseStudySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExamWarehouseStudyVo> responseVos = this.selectList(examWarehouseStudySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public ExamWarehouseStudyVo selectVoByStudyId(Long studyId) {
        return mapper.selectVoByStudyId(studyId);
    }

    @Override
    public ExamDoingVo start(ExamStartVo examStartVo) {
        ExamWarehouseStudy examWarehouseStudy = this.selectByStudyId(examStartVo.getStudyId());
        if (Objects.isNull(examWarehouseStudy)) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此学习模块还未关联题库");
        }
        Date startTime = examWarehouseStudy.getStartTime();
        if (new Date().before(startTime)) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "还未到考核开始时间");
        }
        examStartVo.setWarehouseId(examWarehouseStudy.getWarehouseId());
        return examPaperService.start(examStartVo);
    }

    @Override
    public ExamWarehouseStudy selectByStudyId(Long studyId) {
        return mapper.selectByStudyId(studyId);
    }

}